package org.snusmumr.android.media.data;

import org.snusmumr.android.media.Constants;
import org.snusmumr.android.media.data.DatabaseProvider.DatabaseMode;
import org.snusmumr.android.media.util.Utils;

import android.content.ContentValues;
import android.content.Context;
import android.widget.Toast;

public class UnlentItemsOperation extends DBOperation<Boolean> {
	private long[] selectedItems;

	public UnlentItemsOperation(Context context, long[] selectedItems) {
		super(context);
		this.selectedItems = selectedItems;
	}
	
	@Override
	protected DatabaseMode getDatabaseMode() {
		return DatabaseMode.Write;
	}

	@Override
	public Boolean execute() {
		if(selectedItems == null || selectedItems.length == 0) {
			Toast.makeText(getContext(), "_No items selected", Toast.LENGTH_SHORT).show();
			return Boolean.FALSE;
		} else {
			ContentValues values = new ContentValues();
			values.put(MediaColumns.LEND_DATE, (String)null);
			values.put(MediaColumns.LEND_TO, (String)null);
			String whereClause = MediaColumns._ID + 
			" in(" + Utils.arrayToString(selectedItems) + ')';
			getDatabaseProvider().getDatabase(getDatabaseMode()).update(Constants.DB_TABLE_MEDIA_ITEM, 
						values, 
						whereClause, 
						null);
			
			new FriendCollectorOperation(getContext()).execute();
			
			return Boolean.TRUE;
		}
	}

}
